package khs.java_juniversalchardet_charset;

import java.io.UnsupportedEncodingException;

import org.mozilla.universalchardet.UniversalDetector;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws UnsupportedEncodingException
    {
        String sample_text = "가나";
        byte[] sample_text_byte_array = sample_text.getBytes();
        
        for(int i=0;i<sample_text_byte_array.length;i++){
            System.out.print(String.format("0x%02X", sample_text_byte_array[i]) + " ");
        }
        System.out.println("");
        
        UniversalDetector universalDetector = new UniversalDetector(null);
        universalDetector.handleData(sample_text_byte_array, 0, sample_text_byte_array.length);
        universalDetector.dataEnd();
        
        String sample_charset = universalDetector.getDetectedCharset();
        
        System.out.println(sample_text + ": " + sample_charset);
        
        byte[] encode_text_byte_array = sample_text.getBytes("EUC-KR");
        StringBuilder encode_string_builder = new StringBuilder();
        
        for(int i=0;i<encode_text_byte_array.length;i++){
            System.out.print(String.format("0x%02X", encode_text_byte_array[i]) + " ");
            encode_string_builder.append((char)encode_text_byte_array[i]);
        }
        System.out.println("");
        
        universalDetector.reset();
        universalDetector.handleData(encode_text_byte_array, 0, encode_text_byte_array.length);
        universalDetector.dataEnd();
        
        String encode_charset = universalDetector.getDetectedCharset();
        
        System.out.println(encode_string_builder.toString() + ": " + encode_charset);
    }
}
